package com.yhb.map.sensor;

import com.yhb.map.location.LocationService;
import com.yhb.map.log.Log;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.LocationManager;

public class SensorService implements SensorEventListener{
    
	private Context context;
	private SensorManager sensorManager;
	private Sensor sensor;
	private LocationService locationService;
	
	public SensorService(Context context, LocationService locationService) {
	    this.context = context;
	    sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
	    this.locationService = locationService;
	}
	
	@SuppressWarnings("deprecation")
	public void SetSensorType(int type) {
		//sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		sensor = sensorManager.getDefaultSensor(type);
	}
	
	public void setOnSensorStateChangeListener() {
		sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
	}
	
	public void removeSensorStateChangeListener() {
		sensorManager.unregisterListener(this, sensor);
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		// TODO Auto-generated method stub

	}
	
	private float lastdirection = 0.0F;

	@Override
	public void onSensorChanged(SensorEvent event) {
		float direction = event.values[0];
		if(Math.abs(direction - lastdirection) >= 10) {	
		    locationService.updateArrowDirection(direction);
		    lastdirection = direction;
		}	
	}
	
}
